﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web.UI.HtmlControls;

using AjaxControlToolkit;
using BizElements.Core;
using BizElements.BusinessLayer;

namespace BizElements.Web
{
    /// <summary>Represents a parent binding to <see cref="ListControl"/> control.</summary>
    /// <typeparam name="TParentTable">Metadata class for table that contains parent records.</typeparam>
    public sealed class ParentListBinding<TParentTable> : ParentBindingBase<TParentTable>, IParentBinding
        where TParentTable : IDbTable, new()
    {
        ICodeListProvider codeListProvider;

        #region CTor.

        /// <summary>Initializes a new instance of ParentListBinding class.</summary>
        /// <param name="codeListProvider">Code list provider.</param>
        /// <param name="parentEntityDescriptor">Parent entity descriptor. Additional features are available for composite entities.</param>
        /// <param name="foreignKeyField">FK field.</param>
        /// <param name="boundListControl">List control bound to FK field.</param>
        /// <param name="parentFormUrl">Parent form URL. Required if create new parent and open parent details are used.</param>
        /// <param name="openParentDetailsControl">Optional open parent details control.</param>
        /// <param name="createNewParentControl">Optional create new parent control.</param>
        public ParentListBinding(ICodeListProvider codeListProvider, IDataObjectDescriptor parentEntityDescriptor, IDbColumn foreignKeyField, IBoundListControl boundListControl, string parentFormUrl, Control openParentDetailsControl, Control createNewParentControl)
            : base(parentEntityDescriptor, foreignKeyField, boundListControl, parentFormUrl, openParentDetailsControl, createNewParentControl, false)
        {
            if (codeListProvider == null)
                throw new ArgumentNullException("codeListProvider");

            this.codeListProvider = codeListProvider;
        }

        /// <summary>Initializes a new instance of ParentListBinding class.</summary>
        /// <param name="codeListProvider">Code list provider.</param>
        /// <param name="parentEntityDescriptor">Parent entity descriptor. Additional features are available for composite entities.</param>
        /// <param name="foreignKeyField">FK field.</param>
        /// <param name="boundListControl">List control bound to FK field.</param>
        /// <param name="parentFormUrl">Parent form URL. Required if create new parent and open parent details are used.</param>
        /// <param name="openParentDetailsControl">Optional open parent details control.</param>
        /// <param name="createNewParentControl">Optional create new parent control.</param>
        /// <param name="canClearSelection">Indicates whether bound control should render UI that allows users to clear selection or select a <b>null</b> value.</param>
        public ParentListBinding(ICodeListProvider codeListProvider, IDataObjectDescriptor parentEntityDescriptor, IDbColumn foreignKeyField, IBoundListControl boundListControl, string parentFormUrl, Control openParentDetailsControl, Control createNewParentControl, bool canClearSelection)
            : base(parentEntityDescriptor, foreignKeyField, boundListControl, parentFormUrl, openParentDetailsControl, createNewParentControl, canClearSelection)
        {
            if (codeListProvider == null)
                throw new ArgumentNullException("codeListProvider");

            this.codeListProvider = codeListProvider;
        }

        #endregion

        #region GetCodeList.

        /// <summary>Returns code list.</summary>
        /// <param name="actor">Actor requesting data.</param>
        /// <returns>A code list.</returns>
        public override ICodeList GetCodeList(IActor actor)
        {
            return this.codeListProvider.FetchCodeList(actor);
        }

        #endregion
    }
}
